//
//  PywCommonPlatformInfo.m
//  PywCommonSDKDemo
//
//  Created by Ampaw on 2017/8/11.
//  Copyright © 2017年 Ampaw. All rights reserved.
//

#import "PywCommonPlatformInfo.h"
#import "PywCommonDeviceInfo.h"
#import "PywCommonClientConst.h"
#import "PywCommonMutableDictionary.h"

@implementation PywCommonPlatformInfo

+ (instancetype)sharedCommonPlatformInfo
{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[[self class] alloc] init];
    });
    return instance;
}

#pragma mark - SDK_Type and Channel_Id
- (NSString *)sdk_type
{
    if (!_sdk_type) {
        _sdk_type = SDKTYPE;
    }
    return _sdk_type;
}
- (NSString *)channel_id
{
    if (!_channel_id) {
        _channel_id = [[self promoParamsDictionary] objectForKey:@"channel_id"];
        
        if (_channel_id.length == 0) {
            _channel_id = Current_ChannelID;
        }
    }
    return _channel_id;
}
- (NSString *)channel_code
{
    if (!_channel_code) {
        _channel_code = [[self promoParamsDictionary] objectForKey:@"channel_code"];
    }
    return _channel_code;
}
/// 分包时，子渠道gameKey参数
- (NSString *)subGamekey
{
    if (!_subGamekey) {
        _subGamekey = [[self promoParamsDictionary] objectForKey:@"gamekey"];
    }
    return _subGamekey;
}

#pragma mark - promo channel or code 
- (NSString *)promoChannel
{
    if (!_promoChannel) {
        _promoChannel = [[self promoParamsDictionary] objectForKey:@"promo_channel"];
    }
    return _promoChannel;
}
- (NSString *)promoCode
{
    if (!_promoCode) {
        _promoCode = [[self promoParamsDictionary] objectForKey:@"promo_code"];
    }
    return _promoCode;
}
- (NSDictionary *)promoParamsDictionary
{
    PywCommonMutableDictionary *promoParamsDictionary = [PywCommonMutableDictionary dictionary];
    // 拼接路径
    NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Frameworks/PywCommonSDK.framework"];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    NSURL *url = [bundle URLForResource:@"pyw_promo" withExtension:@"plist"];
    
    // 判断“完整的url字符串（url.absoluteString）”长度大于7，即file:// + 路径
    if (url.absoluteString.length > 7) {
        
        // 将URL中的百分号形式转换成Unicode形式
        NSString *encodeURL = [url.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        // 字符串截取，接受对象字符串范围是给定索引index到这个字符串的结尾。即去除文件协议：file:// ,获得文件路径
        NSString *plistPath = [encodeURL substringFromIndex:7];
        
        // 获取plist文件中的值
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
        if (dictionary) {
            //获取sdktype
            NSString *pyw_sdk_type = dictionary[@"pyw_sdk_type"];
            _sdk_type = pyw_sdk_type;
            [promoParamsDictionary setValue:pyw_sdk_type forKey:@"pyw_sdk_type"];
            
            // 获取plist文件中，字典的Key为channel_id的值
            NSString *channel_id_value = dictionary[@"channel_id"];
            _channel_id = channel_id_value;
            [promoParamsDictionary setValue:channel_id_value forKey:@"channel_id"];
            
            // 获取plist文件中，字典的Key为channel_code的值
            NSString *channel_code_value = dictionary[@"channel_code"];
            _channel_code = channel_code_value;
            [promoParamsDictionary setValue:channel_code_value forKey:@"channel_code"];
            
            // 获取plist文件中，字典的Key为gamekey的值
            NSString *gamekey_value = dictionary[@"gamekey"];
            [promoParamsDictionary setValue:gamekey_value forKey:@"gamekey"];
            
            // 获取plist文件中，字典的Key为pyw_promo的值
            NSString *pyw_promo_value = dictionary[@"pyw_promo"];
            if (pyw_promo_value.length == 0) {
                _promoCode = @"";
                _promoChannel =@"";
                [promoParamsDictionary setValue:@"" forKey:@"promo_code"];
                [promoParamsDictionary setValue:@"" forKey:@"promo_channel"];
                return promoParamsDictionary;
            }
            
            // 当pyw_promo这个key对应的值是以pyw_promo开头时，截取pyw_promo与promo_channel的值
            if ([[pyw_promo_value substringToIndex:9] isEqualToString:@"pyw_promo"]) {
                
                // 以pyw_promo_value这个字符串中的最后一个 . 截取一个新的字符串
                NSString *promoParams = [[pyw_promo_value componentsSeparatedByString:@"."] lastObject];
                
                // 将字符串promoParams以 - 切割为数组
                NSArray *promos = [promoParams componentsSeparatedByString:@"-"];
                
                // 从数组中取值
                if (promos.count > 0) {
                    NSString *chanel = [promos firstObject]; // 第一个为promo_code对应的值
                    [promoParamsDictionary setValue:chanel forKey:@"promo_code"];
                    _promoCode = chanel;
                }
                if (promos.count > 1) {
                    NSString *code = [promos lastObject];    // 最后一个为promo_channel对应的值
                    _promoChannel = code;
                    [promoParamsDictionary setValue:code forKey:@"promo_channel"];
                }
            }
        }
    }
    COMLog(@"promo_Dictionary = %@",promoParamsDictionary.description);
    return promoParamsDictionary;
}

/**
 SDK 版本号
 */
- (NSString *)ver
{
    if (!_ver) {
        _ver = VERSION;
    }
    return _ver;
}

/**
 请求流水号
 */
- (NSString *)tid
{
    _tid =[[NSUUID UUID] UUIDString];
    if (_tid.length >= 16) {
        _tid = [[_tid stringByReplacingOccurrencesOfString:@"-" withString:@""] substringToIndex:16];
    }
    return _tid;
}
- (long)getNowTimeStamp{
    long time;
    NSDate *fromdate = [NSDate date];
    time = (long)[fromdate timeIntervalSince1970];
    return time;
}

/// 通常使用这个作为版本号
- (NSString *)gameVersionCode
{
    if (!_gameVersionCode) {
        _gameVersionCode = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    }
    return _gameVersionCode;
}

/// 通常为 build version
- (NSString *)gameVersionName
{
    if (!_gameVersionName) {
        _gameVersionName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    }
    return _gameVersionName;
}

/// 通常为 bundle identifier
- (NSString *)gameBundleID
{
    if (!_gameBundleID) {
        _gameBundleID = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    }
    return _gameBundleID;
}

/// 通常为 应用程序名称
- (NSString *)gameAppName
{
    if (!_gameAppName) {
        _gameAppName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    }
    return _gameAppName;
}

@end
